package com.zzty.txj.util;

import net.sf.json.JSONObject;

import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class WechatUtil {

    static String appid="wxd230d51f27b1c93a";
    static String secretid="2c84863fd5e3990e9aaf11e0fec684b1";
    public static void main(String[] args){

    }

    public static String getAccessToken() throws IOException {
        System.out.println("get token");
        String accessToken="";
        String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secretid;
        URL url2=new URL(url);
        HttpsURLConnection con=(HttpsURLConnection) url2.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        con.setDoOutput(true);
        con.setDoInput(true);

        con.connect();
        InputStream input=con.getInputStream();
        byte[] c=new byte[input.available()];
        input.read(c);
        // String有一个构造方法，可以接受一个，字节的数组，来创建一个String.
        //这个过程，就实现了，字节数组转化为String的过程。
        String message=new String(c,"UTF-8");
        input.close();
        con.disconnect();

        JSONObject demoJson;
        System.out.println("message  "+message);
        demoJson = JSONObject.fromObject(message);
        accessToken=demoJson.getString("access_token");


        return accessToken;

    }


    public static String getOpenId(String code){
        String openId="";
        String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secretid+"&code="+code+"&grant_type=authorization_code";
        URL urlGet;
        try {
            urlGet = new URL(url);
            HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();

            http.setRequestMethod("GET"); //必须是get方式请求
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("javax.net.ssl.keyStore", "");
            System.setProperty("javax.net.ssl.keyStorePassword", "");
            System.setProperty("javax.net.ssl.trustStore", "");
            System.setProperty("javax.net.ssl.trustStorePassword","");
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
            http.connect();
            InputStream is =http.getInputStream();
            int size =is.available();
            byte[] jsonBytes =new byte[size];
            is.read(jsonBytes);
            String message=new String(jsonBytes,"UTF-8");
            JSONObject demoJson =  JSONObject.fromObject(message);
            System.out.println("demoJson "+demoJson);
            openId = demoJson.getString("openid");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return openId;
    }

}
